<template>
  <div class="el-div">
    <div class="el-div el-s">
      <el-input v-model="input" placeholder="请输入内容" size="mini"></el-input>
    </div>
    <div class="el-div">
      <el-button type="primary" icon="el-icon-search" size="mini"
      >搜索
      </el-button
      >
    </div>
    <div class="el-div">
      <el-button @click="cz" size="mini">重置</el-button>
    </div>
    <div class="el-div">
      <el-button
        type="primary"
        @click="
          addProjectDialog()
        "
        size="mini"
      >添加项目
      </el-button
      >
      <el-dialog :title="title" :visible.sync="dialogFormVisible" width="30%">
        <el-form>
          <el-form-item label="项目名称：" :label-width="formLabelWidth">
            <el-input
              v-model="projectName"
              autocomplete="off"
              placeholder="请输入项目名称"
              size="mini"
            ></el-input>
          </el-form-item>
          <el-form-item label="项目备注：" :label-width="formLabelWidth">
            <el-input
              v-model="description"
              autocomplete="off"
              placeholder="请输入备注"
              size="mini"
            ></el-input>
          </el-form-item>
          <el-form-item label="钉钉链接地址：" :label-width="formLabelWidth">
            <el-input
              v-model="dingDingTalkUrl"
              autocomplete="off"
              placeholder="请输入钉钉链接地址"
              size="mini"
            ></el-input>
          </el-form-item>
          <el-form-item label="钉钉链接密钥：" :label-width="formLabelWidth">
            <el-input
              v-model="dingDingAutograph"
              autocomplete="off"
              placeholder="请输入钉钉链接密钥"
              size="mini"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false" size="mini"
          >取 消
          </el-button
          >
          <el-button
            type="primary"
            @click="projectSubmit()"
            size="mini"
          >确 定
          </el-button
          >
        </div>
      </el-dialog>
    </div>
    <el-table :data="categoryList.data.list" border size="mini">
      <el-table-column
        type="index"
        label="序号"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="projectName"
        label="测试项目"
        width="300"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="description"
        label="测试描述"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="300"
        align="center"
        :formatter="createTimeFormatter"
      ></el-table-column>
      <el-table-column
        prop="updateTime"
        label="更新时间"
        width="300"
        align="center"
        :formatter="updateTimeFormatter"
      ></el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
          >编辑
          </el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      layout="total,sizes,prev,pager,next,jumper"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="categoryList.data.pageSize"
      :current-page="categoryList.data.pageNumber"
      :total="categoryList.data.total"
      style="float: right; margin-top: 10px; margin-bottom: 10px"
      :small="true"
    >
    </el-pagination>
  </div>
</template>

<script>
  import axios from "axios";

  export default {
    created() {
      this.getProjectPage({pageNum: 1, pageSize: 10});
    },
    data() {
      return {
        input: "",
        ta: 50,
        categoryList: {
          data: {
            list: [
              {
                id: "",
                projectName: "",
                description: "",
                createTime: "",
                dingDingAutograph: "",
                dingDingTalkUrl: "",
              },
            ],
            pageSize: 10,
            pageNumber: 1,
            total: 10,
          },
        },
        id: 0,
        dialogFormVisible: false,
        dingDingAutograph: "",
        dingDingTalkUrl: "",
        projectName: "",
        description: "",
        projectContent: "",
        title: "添加项目",
        formLabelWidth: "130px",
      };
    },
    methods: {
      createTimeFormatter(row, column) {
        return this.$dateFomat.timestampToTime(row.createTime * 1000);
      },
      updateTimeFormatter(row, column) {
        return this.$dateFomat.timestampToTime(row.updateTime * 1000);
      },
      getProjectPage(p) {
        this.$project
          .projectPage(p)
          .then((res) => {
            if (res.code == 200) {
              this.categoryList = res;
              console.log(res);
            }
          })
          .catch((res) => {
            console.log(res);
          });
      },
      addProjectDialog() {
        this.dialogFormVisible = true
        this.projectName = "";
        this.description = "";
        this.title = "添加项目";
      },
      cz() {
        this.input = "";
      },
      projectSubmit() {
        this.dialogFormVisible = false
        if (this.title == '添加项目') {
          this.$project
            .projectAdd({
              projectName: this.projectName,
              description: this.description,
              dingDingAutograph: this.dingDingAutograph,
              dingDingTalkUrl: this.dingDingTalkUrl,
            })
            .then((res) => {
              if (res.code == 200) {
                this.$message.success(res.message);
              } else {
              }
              this.getProjectPage({
                pageNum: this.categoryList.data.pageNum,
                pageSize: this.categoryList.data.pageSize,
              });
            })
            .catch((res) => {
              this.$message.error(res.message);

            });
        } else if (this.title == "修改项目") {
          this.$project.projectUpdate({
            id: this.id,
            projectName: this.projectName,
            description: this.description,
            dingDingAutograph: this.dingDingAutograph,
            dingDingTalkUrl: this.dingDingTalkUrl,
          }).then(res => {
            if (res.code == 200) {
              this.$message.success(res.message);
            } else {
            }
            this.getProjectPage({
              pageNum: this.categoryList.data.pageNum,
              pageSize: this.categoryList.data.pageSize,
            });
          })
            .catch((res) => {
              this.$message.error(res.message);
            });
        }

      },
      handleEdit(index, value) {
        this.projectName = value.projectName;
        this.description = value.description;
        this.dingDingAutograph = value.dingDingAutograph;
        this.dingDingTalkUrl = value.dingDingTalkUrl;
        this.dialogFormVisible = true;
        this.title = "修改项目";
        this.id = value.id
      },
      handleSizeChange(val) {
        this.getProjectPage({pageNum: 1, pageSize: val})
      },
      handleCurrentChange(val) {
        this.getProjectPage({
          pageNum: val,
          pageSize: this.categoryList.data.pageSize,
        })
      },
      handleDelete(index) {
        let id = this.categoryList.data.list[index].id;
        this.$project
          .projectDelete({id: id})
          .then((res) => {
            console.log(res);
            this.getProjectPage({
              pageNum: this.categoryList.data.pageNum,
              pageSize: this.categoryList.data.pageSize,
            })
          })
          .catch((res) => {
            console.log(res);
          });
      },
    },
  };
</script>
<style>
  .el-div {
    float: left;
    margin-left: 20px;
    margin-top: 10px;
    margin-bottom: 10px;
  }

  .el-s {
    margin-left: 0px;
  }

  .el-col {
    border-radius: 4px;
  }
</style>
